package projectjet;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class AuthService {	
	private static List<String> users = new ArrayList<String>();
	private static String userPassword = "";
	
	public static List<String> connection() {
		
		 Connection conn = null;

         try
         {             
             String dbName = "MultiChat";
             String url = "jdbc:derby:" + dbName + ";create=true";
             Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance ();
             conn = DriverManager.getConnection(url);
             String createString = "SELECT * FROM Autentification";
             PreparedStatement stmt = conn.prepareStatement(createString);
          
             ResultSet rs = stmt.executeQuery(); 
             while (rs.next()) {
                    String name = rs.getString("name");
                    String password = rs.getString("password");
                    userPassword = name + " "+ password;
                    users.add(userPassword);
             }
         }
         catch (Exception e)
         {
             System.err.println ("Cannot connect to database server");
             e.printStackTrace();
         }
         finally
         {
             if (conn != null)
             {
                 try
                 {
                     conn.close ();
                 }
                 catch (Exception e) {
                	 System.out.println("Error with closing");
                 }
             }
         }

		
		return users;
	}
	
	public boolean checkSecurity(String user){
		
		for(String element: users)
			if (element.equals(user)) return true;
		
		return false;
	}
	

}
